package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpExportDetail;
import com.jims.master.modules.csm.entity.ExpExportMaster;
import com.jims.master.modules.csm.entity.ExpInfo;
import com.jims.master.modules.csm.entity.ExpStock;
import com.jims.master.modules.csm.service.ExpExportService;
import com.jims.master.modules.csm.service.ExpStorageDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.utils.ExpUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 消耗品出库处理Controller
 * @author fengyg
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expExport")
public class ExpExportController extends BaseController{

    @Autowired
    private ExpExportService expExportService;
    @Autowired
    private ExpStorageDeptService expStorageDeptService;

    /**
     * 出库处理
     * @param expExportDetail
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ExpExportDetail expExportDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpExportDetail> page = expExportService.findPage(new Page<ExpExportDetail>(request, response), expExportDetail);
        model.addAttribute("page", page);
        return "modules/csm/expExportIndex";
    }


    /**
     * 出库单据查询
     * @param expExportMaster
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String listque(ExpExportMaster expExportMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpExportMaster> page = expExportService.findExportList(new Page<ExpExportMaster>(request, response), expExportMaster);
        Date date = expExportMaster.getCreateDate();
        Date date1 = expExportMaster.getUpdateDate();

        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date != null) {
            expExportMaster.setCreateDateStr(dateformat.format(date));
        }
        if (date1 != null) {
            expExportMaster.setUpdateDateStr(dateformat.format(date1));
        }
        model.addAttribute("expExportMaster",expExportMaster);
        model.addAttribute("page", page);
        return "modules/csm/expExportQueryIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ExpExportDetail expExportDetail, Model model) {
        model.addAttribute("expExportDetail", expExportDetail);
        return "modules/csm/expExportForm";
    }

    /**
     * @param expInfo 传递参数
     * @return List<ExpInfo> 返回类型
     * @throws
     * @Title: autoCompletePrice
     * @Desciption: (消耗品出库选择有库存有价格的消耗品自动补全)
     * @author fengyg
     * @date 2017-03-17
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoExportStock", ""})
    @ResponseBody
    public List<ExpExportDetail> autoExportStock(String q, String limit, ExpInfo expInfo) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<ExpExportDetail> list = expExportService.autoExportStock(expInfo,UserUtils.getOrgId(), q, limit);
        return list;
    }

    /**
     * 消耗品出库处理
     * @param detailList
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveExport")
    @ResponseBody
    public StringData save(@RequestBody List<ExpExportDetail> detailList,HttpServletRequest request,HttpServletResponse response) {
        StringData data = new StringData();
        String dataString= expExportService.saveExportList(detailList);
        if(dataString=="-1"){
            data.setCode("error");
            data.setData("没有需要保存的数据!");
        }
        else if(dataString=="0"){
            data.setCode("error");
            data.setData("出库失败!");
        }
        else {
            data.setCode("success");
            data.setData(dataString);
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ExpExportDetail expExportDetail) {
        StringData data = new StringData();
        //int i = ExpExportService.delete(expExportMaster);
        //if (i > 0) {
        //    data.setCode("success");
        //    data.setData("删除消耗品出库成功");
        //} else {
        //    data.setCode("error");
        //    data.setData("删除消耗品出库失败");
        //}
        return data;
    }

    /**
     * 根据供货方ID(storageId)查询需要批量入库的数据
     * @param storageId 供货方ID
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getBatchImportListData", ""})
    @ResponseBody
    public List<ExpStock> getBatchImportList(String storageId, HttpServletRequest request, HttpServletResponse response, Model model) {
        return expExportService.getBatchImportList(storageId);
    }

    /**
     * 根据传递过来的入出库类别返回供货方集合
     * @param type     入出库类别,如果是退消耗，那么返回生产商供应商集合;如果是其他,返回库房集合
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getSupplierList", ""})
    @ResponseBody
    public List<ExpExportDetail> getSupplierList(String type, HttpServletRequest request, HttpServletResponse response, Model model) {
        List list =  ExpUtils.getSupplierOrStorageList(type);
        return list;
    }
}
